<template>
  <div>
    <!-- 头部面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 头部面包屑导航 -->
    <!-- 卡片显示区域 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="onAddCateClicked"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 添加分类按钮 -->
      <!-- 表格区域 -->
      <tree-table
        class="cate_table"
        show-index
        border
        :data="catelist"
        index-text=""
        :selection-type="false"
        :expand-type="false"
        :columns="columns"
      >
        <!-- 是否有效 isok -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-error"
            style="color:red"
            v-if="scope.row.cat_deleted"
          ></i>
          <i class="el-icon-success" style="color:lightgreen" v-else></i>
        </template>
        <!-- 是否有效 isok -->
        <!-- 排序 order -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 排序 order -->
        <!-- 操作 opt -->
        <template slot="opt">
          <el-button size="mini" type="primary" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
        <!-- 操作 opt -->
      </tree-table>
      <!-- 表格区域 -->
      <!-- 底部分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 底部分页区域 -->
    </el-card>
    <!-- 卡片显示区域 -->
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="handleAddCateDialogClose"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="cateParentKeys"
            :options="parentCateList"
            :props="cateParentProps"
            @change="handleCateParentChange"
            @visible-change="handleCateChooseChange"
            clearable
          >
          </el-cascader>
          <!-- <el-cascader-panel
            :options="parentCateList"
            :props="cateParentProps"
          ></el-cascader-panel> -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onAddCateOKClicked">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加分类对话框 -->
  </div>
</template>

<script>
export default {
  created() {
    this.getCateList()
  },
  data() {
    return {
      catelist: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [],
      cateParentKeys: [],
      cateParentProps: {
        expandTrigger: 'hover',
        checkStrictly: 'true',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$msg.error(res.meta.status + res.meta.msg)
      }
      this.catelist = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(size) {
      this.queryInfo.pagesize = size
      this.getCateList()
    },
    handleCurrentChange(page) {
      this.queryInfo.pagenum = page
      this.getCateList()
    },
    onAddCateClicked() {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    async getParentCateList() {
      const { data: res } = await this.$http.get('/categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$msg.error(res.meta.status + res.meta.msg)
      }
      this.parentCateList = res.data
      // console.log(this.parentCateList)
    },
    handleCateParentChange() {
      // console.log(this.cateParentKeys)
      let len = this.cateParentKeys.length
      if (len > 0) {
        this.addCateForm.cat_pid = this.cateParentKeys[len - 1]
        this.addCateForm.cat_level = len
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    handleCateChooseChange(v) {
      // console.log(v)
      if (v) {
        document.querySelector('.el-cascader-panel').style.height = '200px'
      }
    },
    onAddCateOKClicked() {
      // console.dir(this.addCateForm)
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post(
          'categories',
          this.addCateForm
        )
        if (res.meta.status !== 201) {
          return this.$msg.error(res.meta.status + res.meta.msg)
        }
        this.$msg.success(res.meta.msg)
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    handleAddCateDialogClose() {
      this.$refs.addCateFormRef.resetFields()
      this.cateParentKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    }
  }
}
</script>
<style lang="less" scoped>
.cate_table {
  margin: 15px 0;
}
// .el-popper {
//   max-height: 200px !important;
// }
.el-cascader-panel {
  height: 200px !important;
}

.el-cascader {
  width: 100%;
  height: 100%;
  line-height: 40px !important;

  // height: 40px;
}

.el-input__inner {
  height: 40px !important;
}
</style>
